<?php

/**
 * @category   Zhishidi daily deals
 * @package    Zhishidi_dailydeals
 * @author     ketu.lai
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class Zhishidi_Dailydeals_Block_Dailydeals extends Mage_Core_Block_Template {
    const XML_DAILY_DEALS_MODEL_PATH = 'zhishidi_dailydeals/settings';
    protected $_priceBlock = array();
    protected $_block = 'catalog/product_price';
    protected $_priceBlockDefaultTemplate = 'catalog/product/price.phtml';
    protected $_tierPriceDefaultTemplate = 'catalog/product/view/tierprices.phtml';
    protected $_priceBlockTypes = array();

    protected function _construct() {
        parent::_construct();
    }

    public function loadDailyDealsCollection() {
        $collection = Mage::getModel('zhishidi_dailydeals/deals')->getCollection()
                        ->joinProducts()
                        ->addStoreFilter(Mage::app()->getStore()->getId())
                        ->addVisibleFilter()
                        ->setDateRange(date('Y-m-d H:i:s', Mage::app()->getLocale()->storeTimeStamp()))
                        ->addFieldToFilter('deal_ended', array('null' => true))
                        ->setPageSize($this->getSettings()->getMaxCount())
                        ->setCurPage(1)
                        ->setOrder('deal_from', 'ASC');
       
        return $collection;
    }

    protected function _prepareLayout() {
        if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
            $breadcrumbsBlock->addCrumb($this->__('Home'), array(
                'label' => $this->__('Home'),
                'title' => $this->__('Back To Home Page'),
                'link' => $this->getUrl(),
                'readonly' => true,
            ));
            $breadcrumbsBlock->addCrumb($this->getSettings()->getPageTitle(), array('label' => $this->getSettings()->getPageTitle()));
        }
        parent::_prepareLayout();
    }

    public function getGalleryUrl($product, $image=null) {
        $params = array('id' => $product->getId());
        if ($image) {
            $params['image'] = $image->getValueId();
            return $this->getUrl('catalog/product/gallery', $params);
        }
        return $this->getUrl('catalog/product/gallery', $params);
    }

    protected function _getPriceBlock($productTypeId) {
        if (!isset($this->_priceBlock[$productTypeId])) {
            $block = $this->_block;
            if (isset($this->_priceBlockTypes[$productTypeId])) {
                if ($this->_priceBlockTypes[$productTypeId]['block'] != '') {
                    $block = $this->_priceBlockTypes[$productTypeId]['block'];
                }
            }
            $this->_priceBlock[$productTypeId] = $this->getLayout()->createBlock($block);
        }
        return $this->_priceBlock[$productTypeId];
    }

    protected function _getPriceBlockTemplate($productTypeId) {
        if (isset($this->_priceBlockTypes[$productTypeId])) {
            if ($this->_priceBlockTypes[$productTypeId]['template'] != '') {
                return $this->_priceBlockTypes[$productTypeId]['template'];
            }
        }
        return $this->_priceBlockDefaultTemplate;
    }

    /**
     * Prepares and returns block to render some product type
     *
     * @param string $productType
     * @return Mage_Core_Block_Template
     */
    public function _preparePriceRenderer($productType) {
        return $this->_getPriceBlock($productType)
                ->setTemplate($this->_getPriceBlockTemplate($productType))
                ->setUseLinkForAsLowAs($this->_useLinkForAsLowAs);
    }

    /**
     * Returns product price block html
     *
     * @param Mage_Catalog_Model_Product $product
     * @param boolean $displayMinimalPrice
     * @param string $idSuffix
     * @return string
     */
    public function getPriceHtml($product, $displayMinimalPrice = false, $idSuffix = '') {
        return $this->_preparePriceRenderer($product->getTypeId())
                ->setProduct($product)
                ->setDisplayMinimalPrice($displayMinimalPrice)
                ->setIdSuffix($idSuffix)
                ->toHtml();
    }

    protected function getSettings() {
        return Mage::getModel(self::XML_DAILY_DEALS_MODEL_PATH);
    }

}